package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_link;
import cn.com.api.entity.ApiU_link_group;
import cn.com.api.service.ApiU_linkService;
import cn.com.api.service.ApiU_link_groupService;
import cn.com.util.*;
import cn.com.util.*;

public class U_linkBaseController {

	private static Logger logger = Logger.getLogger(U_linkBaseController.class);

	@Autowired
	private ApiU_linkService apiU_linkService;
	@Autowired
	private ApiU_link_groupService apiU_link_groupService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link-list")
	public ModelAndView u_link_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_link_list.jsp");

		List<ApiU_link> u_links = apiU_linkService.findU_links();
		mv.addObject("u_links", u_links);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link-add-pre")
	public ModelAndView u_link_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_link_add.jsp");
		List<ApiU_link_group> grouplist = apiU_link_groupService.findU_link_groups();
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_link u_link = apiU_linkService.findU_linkById(Integer.valueOf(id));
			mv.addObject("u_link", u_link);
		}
		mv.addObject("grouplist", grouplist);
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link-add")
	public void u_link_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String group_id = request.getParameter("group_id");
		String url = request.getParameter("url");
		String is_effect = request.getParameter("is_effect");
		String sort = request.getParameter("sort");
		// String img = request.getParameter("img");
		String description = request.getParameter("description");
		// String count = request.getParameter("count");
		// String show_index = request.getParameter("show_index");
		// String ext_1 = request.getParameter("ext_1");
		// String ext_2 = request.getParameter("ext_2");
		// String ext_3 = request.getParameter("ext_3");
		// String ext_4 = request.getParameter("ext_4");
		// String ext_5 = request.getParameter("ext_5");

		Map<String, Object> map = new HashMap<String, Object>();
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_link u_link = new ApiU_link();
			u_link.setName(name);
			u_link.setGroup_id(group_id);
			u_link.setUrl(url);
			u_link.setIs_effect(Integer.valueOf(is_effect));
			u_link.setSort(Integer.valueOf(sort));
			// u_link.setImg(img);
			u_link.setDescription(description);
			// u_link.setCount(count);
			// u_link.setShow_index(show_index);
			// u_link.setExt_1(ext_1);
			// u_link.setExt_2(ext_2);
			// u_link.setExt_3(ext_3);
			// u_link.setExt_4(ext_4);
			// u_link.setExt_5(ext_5);

			apiU_linkService.insertU_link(u_link);
		}
		// edit
		else {
			ApiU_link u_link = apiU_linkService.findU_linkById(Integer.valueOf(id));
			u_link.setName(name);
			u_link.setGroup_id(group_id);
			u_link.setUrl(url);
			u_link.setIs_effect(Integer.valueOf(is_effect));
			u_link.setSort(Integer.valueOf(sort));
			// u_link.setImg(img);
			u_link.setDescription(description);
			// u_link.setCount(count);
			// u_link.setShow_index(show_index);
			// u_link.setExt_1(ext_1);
			// u_link.setExt_2(ext_2);
			// u_link.setExt_3(ext_3);
			// u_link.setExt_4(ext_4);
			// u_link.setExt_5(ext_5);

			apiU_linkService.updateU_link(u_link);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_link-delete")
	public void u_link_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_linkService.deleteU_link(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
